package com.wangshouyu.mp.offiaccount;

import com.google.common.io.ByteStreams;
import com.wangshouyu.mp.base.request.HttpMethod;
import com.wangshouyu.mp.base.request.MpClient;
import com.wangshouyu.mp.base.request.RequestBody;
import com.wangshouyu.mp.base.response.BaseResponse;
import com.wangshouyu.mp.utils.GsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.text.MessageFormat;

import static com.wangshouyu.mp.base.constant.UrlConstant.*;

/**
 * @author 王守钰
 * @date 2020年12月07日 12:47
 * @description 客服账号处理
 */
public class AccountHandler {

    private AccountHandler() {
    }

    protected static final Logger log = LoggerFactory.getLogger(AccountHandler.class);

    /**
     * 添加客服员
     * @param client 客户端
     * @param account 账号
     * @param accessToken 秘钥
     * @return 添加结果
     */
    public static BaseResponse add(MpClient client, Account account, String accessToken){
        String url = MessageFormat.format(ADD_ACCOUNT_URL, accessToken);
        return client.request(
                HttpMethod.POST,
                url,
                RequestBody.create(GsonUtils.toJson(account)),
                BaseResponse.class);
    }

    /**
     * 修改客服员
     * @param client 客户端
     * @param account 账号
     * @param accessToken 秘钥
     * @return 修改结果
     */
    public static BaseResponse update(MpClient client, Account account, String accessToken){
        String url = MessageFormat.format(UPDATE_ACCOUNT_URL, accessToken);
        return client.request(
                HttpMethod.POST,
                url,
                RequestBody.create(GsonUtils.toJson(account)),
                BaseResponse.class);
    }

    /**
     * 删除客服员
     * @param client 客户端
     * @param account 账号
     * @param accessToken 秘钥
     * @return 删除结果
     */
    public static BaseResponse del(MpClient client, Account account, String accessToken){
        String url = MessageFormat.format(DEL_ACCOUNT_URL, accessToken);
        return client.request(
                HttpMethod.POST,
                url,
                RequestBody.create(GsonUtils.toJson(account)),
                BaseResponse.class);
    }

    /**
     * 上传头像
     * @param client 客户端
     * @param account 账号
     * @param accessToken 凭证
     * @param inputStream 输入流
     * @param fileName 文件名
     * @return
     * @throws Exception
     */
    public static BaseResponse uploadHeadImage(MpClient client,
                                   String account,
                                   String accessToken,
                                   InputStream inputStream,
                                   String fileName) throws Exception{
        String url = MessageFormat.format(UPLOAD_ACCOUNT_HEADER_IMAGE_URL, accessToken, account);
        return client.request(
                HttpMethod.POST,
                url,
                RequestBody.create(ByteStreams.toByteArray(inputStream),fileName),
                BaseResponse.class);
    }

    /**
     * 客服列表
     * @param client 客户端
     * @param accessToken 凭证
     * @return 客服列表
     */
    public static AccountList list(MpClient client, String accessToken){
        String url = MessageFormat.format(ACCOUNT_LIST_URL, accessToken);
        return client.request(HttpMethod.GET, url, AccountList.class);
    }
}
